package com.payment.controller;
import com.payment.entity.HospitalUser;
import com.payment.service.HospitalUserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/hospital") //医生、管理员接口
public class HospitalUserController {

    @Resource
    public HospitalUserService hospitalUserService;

    //登录
    @PostMapping("/login")
    public ResponseEntity<ResDTO> login(@RequestBody HospitalUser entity){
        HospitalUser res=hospitalUserService.login(entity);
        System.out.println(res);
        if (res!=null){
            return ResponseEntity.ok(ResDTO.success(res));
        }else{
            return ResponseEntity.ok(ResDTO.error("找不到该用户！"));
        }
    }

    //查询所有用户
    @GetMapping("/getAllUser")
    public ResponseEntity<ResDTO> QueryAll() {

        List<HospitalUser> _list = hospitalUserService.getAllUser();

        // 创建一个新的列表来存储未删除的用户
        List<HospitalUser> res = new ArrayList<>();
        if (_list!=null){
            for (HospitalUser user : _list) {
                if (user.getIsDelActive() == 0) { // 只添加未删除的用户
                    res.add(user);
                }
            }
            return ResponseEntity.ok(ResDTO.success(res));
        }
        return ResponseEntity.ok(ResDTO.error());
    }

    //新增
    @PostMapping("/addUser")
    public ResponseEntity<ResDTO> add(@RequestBody HospitalUser entity){
        System.out.println("]]]]]]]]]]]]]]]]");
        System.out.println(entity);

            int res= hospitalUserService.addUser(entity);
            if (res>0){
                return  ResponseEntity.ok(ResDTO.success(null));
            }

        return  ResponseEntity.ok(ResDTO.error());
    }

    //修改  需要id和对象（要修改的对象）
    @PutMapping("/putUser/{id}")
    public ResponseEntity<ResDTO> update(@PathVariable int id,@RequestBody HospitalUser entity){
        HospitalUser table  = hospitalUserService.getUserById(id);
        if (table!=null){
            table.setUserName(entity.getUserName());
            table.setUserPasswd(entity.getUserPasswd());
            table.setJurisdiction(entity.getJurisdiction());
            table.setNickName(entity.getNickName());
            table.setDoctorDescribe(entity.getDoctorDescribe());
            hospitalUserService.editUser(table);

            return ResponseEntity.ok(ResDTO.success(table));
        }else {
            return ResponseEntity.ok(ResDTO.error("更新失败！"));
        }
    }

    //删除
    @DeleteMapping("/delUser/{id}")
    public ResponseEntity<ResDTO> remove(@PathVariable int id){

        int res=hospitalUserService.delUser(id);
        if (res!=0){
            return ResponseEntity.ok(ResDTO.success(null));
        }else {
            return ResponseEntity.ok(ResDTO.error("没有找到该用户！删除失败！"));
        }
    }


}

